package com.ntd.frame.haikang.remote;

import cn.hutool.json.JSONObject;
import com.hikvision.artemis.sdk.ArtemisHttpUtil;
import com.hikvision.artemis.sdk.config.ArtemisConfig;

import java.util.HashMap;
import java.util.Map;

public class RemoteHaiKang {

    private final static String ARTEMIS_PATH = "/artemis";
    private static final String HTTP_TYPE = "http://";

    static {
        // artemis网关服务器ip端口
        ArtemisConfig.host = "117.158.156.164:801";
        // appKey
        ArtemisConfig.appKey = "23991440";
        // 秘钥appSecret
        ArtemisConfig.appSecret = "G9MvIH2NnYQ4hLJ1eG6p";
    }

    public static String getStreamUrlByCameraIndexCode(String cameraIndexCode, String streamUrlProtocol) {
        JSONObject params = initParams();
        params.putOnce("cameraIndexCode", cameraIndexCode);
        params.putOnce("protocol", streamUrlProtocol);
        return doPost("/api/video/v1/cameras/previewURLs", params.toString());
    }

    /**
     * 获取区域列表
     * @param parentIndexCode
     * @return
     */
    public static String ListRegionByParentIndexCode(String parentIndexCode) {
        JSONObject params = initParams();
        params.putOnce("parentIndexCode", parentIndexCode);
        return doPost("/api/resource/v1/regions/subRegions", params.toString());
    }

    /**
     * 获取监控设备列表
     * @param regionIndexCode
     * @return
     */
    public static String listCameraByRegionIndexCode(String regionIndexCode) {
        JSONObject params = initParams();
        params.putOnce("regionIndexCode", regionIndexCode);
        return doPost("/api/resource/v1/camera/advance/cameraList", params.toString());
    }

    private static String doPost(String subPath, String params) {
        return ArtemisHttpUtil.doPostStringArtemis(initPath(subPath), params, null, null, "application/json");
    }

    private static JSONObject initParams() {
        JSONObject params = new JSONObject();
        params.putOnce("pageNo", 1);
        params.putOnce("pageSize", 100);
        return params;
    }

    private static Map<String, String> initPath(String subPath) {
        Map<String, String> path = new HashMap<String, String>(2) {{
            // 根据现场环境部署确认是http还是https
            put(HTTP_TYPE, ARTEMIS_PATH + subPath);
        }};
        return path;
    }
}
